//
//  ZUserInfo.m
//  WeClubs
//
//  Created by Zed Link on 5/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZUserInfo.h"

#define USER_ID_KEY @"WECLUBS_USER_ID"
#define USER_TOKEN_KEY @"WECLUBS_USER_TOKEN"

@implementation ZUserInfo

@synthesize userId = _userId;
@synthesize token = _token;

Z_SINGLETON_IMPLEMENTATION(ZUserInfo)

#pragma mark - Getter & Setter

- (void)setUserId:(long)userId
{
    _userId = userId;
    [self saveObject:@(_userId) forKey:USER_ID_KEY];
}

- (long)userId
{
    NSNumber *userIdValue = [[NSUserDefaults standardUserDefaults] objectForKey:USER_ID_KEY];
    return userIdValue == nil ? MAXFLOAT : [userIdValue longValue];
}

- (void)setToken:(NSString *)token
{
    _token = token;
    
    if (token == nil) {
        [self removeObjectForKey:USER_TOKEN_KEY];
    } else{
        [self saveObject:_token forKey:USER_TOKEN_KEY];
    }
}

- (NSString *)token
{
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:USER_TOKEN_KEY];
    return token == nil ? TOKEN_INVALIDATE_DESCRIPTION : token;
}

#pragma mark - Private Method

- (void)saveObject:(id)object forKey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] setObject:object forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)removeObjectForKey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
